<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/6/5
 * Time: 12:52
 */

namespace app\api\controller;
use app\BaseController;
use app\api\models\EscortUserMoneylog;
use app\api\models\EscortUserIntegral;
use think\Request;
use app\api\models\EscortPayPayorder;

class User extends BaseController
{
    public function user_info(Request $request){
        $user = $this->user(true);//更新数据
        if(!$user){
            return $this->message('重新登录', [], 201);
        }
        $id=$user->id;
        $name=$user->nick;
        $headimg=$user->headimg;
        $mobile=$user->userLogin->mobile;
        $money=$user->money; 
        $integral=$user->integral;
        if($headimg){
            $headimg=getFullImageUrl($headimg);
        }else{
            $headimg=getFullImageUrl('/storage/imges/mo.png');
        }
        $arr=array(
            'id' => $id,
            'name' => $name,
            'headimg' => $headimg,
            'mobile' => $mobile,
            'money' => $money,
            'integral' => $integral,
        );
        return $this->message('请求成功', $arr);
    }
    //修改用户昵称
    public function setnick(Request $request){
        $data = $request->post();
        $name=strip_tags(trim($data['name']));//用户昵称
        if(!$name){
            return $this->message('昵称不能为空', [],0);
        }
        $user = $this->user();
        if(!$user){
            return $this->message('重新登录', [], 201);
        }
        $user->nick=$data['name'];
        $user->save();
        return $this->message('修改成功', [], 200);
    }

    //上传用户头像
    public function upload(Request $request){
        $user = $this->user();
        if(!$user){
            return $this->message('重新登录', [], 201);
        }
        //验证码token end
        $result=[];
        $type=input('param.type/d');
        $file = request()->file('file');
        try {
            validate(['file' => [
                'fileSize' => 1048576,
                'fileExt' => 'jpg,png,gif,jpeg',
                'fileMime' => 'image/jpeg,image/png,image/gif',
            ]])->check(['file' => $file]);
            $savename = \think\facade\Filesystem::disk('public')->putFile( 'user_img', $file);
            if (!$savename) {
                return json(['', 'code' => 0, 'message' => '上传失败']);
            }
            $imges='/storage/'.str_replace('\\','/',$savename);
            if(!$type){
                //存入用户头像
                $user->headimg=$imges;
                $user->save();
            }
            $savename=getFullImageUrl($imges);
            return json(['savename' => $savename, 'img'=>$imges,'code' => 200, 'message' => '上传成功']);
        } catch (ValidateException $e) {
            return $e->getMessage();
        }
    }
    //查询系统设置
    public function getsite(){
        $company=getMerConfig('-appsite.app_company');//公司名称
        $version=getMerConfig('-appsite.app_version');//版本号
        $name=getMerConfig('-appsite.app_name');//名称
        $img=getMerConfig('-appsite.app_logoimg');//平台logo
        if($img){
            $img=getFullImageUrl($img);
        }
        $arr=array('name'=>$name,'company'=>$company,'version'=>$version,'img'=>$img);
        return $this->message('修改成功', $arr, 200);
    }
    //查询支付记录
    public function setpaylist(){
        $user = $this->user();
        if(!$user){
            return $this->message('重新登录', [], 201);
        }
        $page=input("param.page/d");
        if(!$page){
            $page=1;
        }
        $type=strip_tags(trim(input("param.type/d")));//
        if(!$type){
            $type=0;
        }
        $uid=$user->id;
        if($type==0){
            //$count=EscortPayPayorder::where('uid',$uid)->where('status','<>',1)->count();
            //$list=EscortPayPayorder::where('uid',$uid)->where('status','<>',1)->order('id desc')->page($page,10)->select();
            //根据app\models\BaseModel.php中的查询多条数据
            $wheres=[];
            $wheres[]=['uid','=',$uid];
            $wheres[]=['status','<>',1];
            $count=EscortPayPayorder::countData(['where'=>$wheres]);
            $list=EscortPayPayorder::findAllData(['where'=>$wheres,'page'=>$page,'limit'=>10,'order'=>'id desc']);
        }else{
            //$count=EscortPayPayorder::where('uid',$uid)->where('status',$type)->count();
            //$list=EscortPayPayorder::where('uid',$uid)->where('status',$type)->order('id desc')->page($page,10)->select();
            //根据app\models\BaseModel.php中的查询多条数据
            $count=EscortPayPayorder::countData(['where'=>['uid'=>$uid,'status'=>$type]]);
            $list=EscortPayPayorder::findAllData(['where'=>['uid'=>$uid,'status'=>$type],'page'=>$page,'limit'=>10,'order'=>'id desc']);
        }
        $data = [
            'count' => $count,
            'list' => $list
        ];
        return $this->message('请求成功', $data);
    }
    //资金记录
    public function moneylog(){
        $user = $this->user();
        if(!$user){
            return $this->message('重新登录', [], 201);
        }
        $page=input("param.page/d");
        if(!$page){
            $page=1;
        }
        $where=[];
        $type=strip_tags(trim(input("param.type/d")));//
        if($type){
            $where['mo_type']=$type;
        }
        $uid=$user->id;
        //$count=EscortUserMoneylog::where('user_id',$uid)->where($where)->count();
        //$list=EscortUserMoneylog::where('user_id',$uid)->where($where)->order('id desc')->page($page,10)->select();
        //根据app\models\BaseModel.php中的查询多条数据
        $where[]=['user_id','=',$uid];
        $count=EscortUserMoneylog::countData(['where'=>$where]);
        $list=EscortUserMoneylog::findAllData(['where'=>$where,'page'=>$page,'limit'=>10,'order'=>'id desc']);
        $data = [
            'count' => $count,
            'list' => $list
        ];
        return $this->message('请求成功', $data);
    }
    //积分记录
    public function integrallog(){
        $user = $this->user();
        if(!$user){
            return $this->message('重新登录', [], 201);
        }
        $page=input("param.page/d");
        if(!$page){
            $page=1;
        }
        $where=[];
        $type=strip_tags(trim(input("param.type/d")));//
        if($type){
            $where['mo_type']=$type;
        }
        $uid=$user->id;
        //$count=EscortUserIntegral::where('user_id',$uid)->where($where)->count();
        //$list=EscortUserIntegral::where('user_id',$uid)->where($where)->order('id desc')->page($page,10)->select();
        //根据app\models\BaseModel.php中的查询多条数据
        $where[]=['user_id','=',$uid];
        $count=EscortUserIntegral::countData(['where'=>$where]);
        $list=EscortUserIntegral::findAllData(['where'=>$where,'page'=>$page,'limit'=>10,'order'=>'id desc']);
        $data = [
            'count' => $count,
            'list' => $list
        ];
        return $this->message('请求成功', $data);
    }
}